package com.hydra.framework.db.cursor;

import android.database.Cursor;

import java.util.HashMap;

/**
 * Created by hydra
 *
 */
public enum CursorFieldType {

	BLOB("BLOB", byte[].class) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getBlob(columnIndex);
		}
	},
	FLOAT("REAL", Float.class) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getFloat(columnIndex);
		}
	},
	PRIMITIVE_FLOAT("REAL", Float.TYPE) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getFloat(columnIndex);
		}
	},
	DOUBLE("REAL", Double.class) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getDouble(columnIndex);
		}
	},
	PRIMITIVE_DOUBLE("REAL", Double.TYPE) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getDouble(columnIndex);
		}
	},
	INT("INT", Integer.class) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getInt(columnIndex);
		}
	},
	PRIMITIVE_INT("INT", Integer.TYPE) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getInt(columnIndex);
		}
	},
	LONG("INT", Long.class) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getLong(columnIndex);
		}
	},
	PRIMITIVE_LONG("INT", Long.TYPE) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getLong(columnIndex);
		}
	},
	STRING("TEXT", String.class) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getString(columnIndex);
		}
	},
	SHORT("INT", Short.class) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getShort(columnIndex);
		}
	},
	PRIMITIVE_SHORT("INT", Short.TYPE) {
		@Override
		public Object getObject(Cursor cursor, int columnIndex) {
			return cursor.getShort(columnIndex);
		}
	};

	public abstract Object getObject(Cursor cursor, int columnIndex);

	public final Class<?> typeClass;
	public final String typeStateMentInSql;

	CursorFieldType(String typeStateMentInSql, Class<?> typeClass) {
		this.typeClass = typeClass;
		this.typeStateMentInSql = typeStateMentInSql;
	}

	private static HashMap<Class<?>, CursorFieldType> allCfs = new HashMap<Class<?>, CursorFieldType>();

	static {
		for(CursorFieldType type : values()) {
			allCfs.put(type.typeClass, type);
		}
	}

	public static CursorFieldType getCursorType(Class<?> cls) {
		return allCfs.get(cls);
	}
}
